package 双指针;


/**
 * https://leetcode.cn/problems/move-zeroes/?envType=study-plan-v2&envId=top-100-liked
 *
 * 题解：https://mp.weixin.qq.com/s?__biz=MzAxODQxMDM0Mw==&mid=2247493087&idx=1&sn=49ffb1aacf14c9f60f304c0d4af55255&chksm=9bd41bd7aca392c1aa0b1aec02d294068dea919fc6e8b8c692b7ba888e853e4
 *
 * 输入: nums = [0,1,0,3,12]
 * 输出: [1,3,12,0,0]
 * 示例 2:
 *
 * 输入: nums = [0]
 * 输出: [0]
 */
public class _283_移动零 {

    public static void main(String[] args) {
        int[] nums = new int[]{0,1,0,3,12};
        _283_移动零 _283_移动零 = new _283_移动零();
        _283_移动零.moveZeroes(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.println("nums[i]:" + nums[i]);
        }
    }

    public void moveZeroes(int[] nums) {
        int length = nums.length;
        int res = removeElement(nums,0);
        for (int i = res;i<length;i++) {
            nums[i] = 0;
        }
    }

    public int removeElement(int[] nums, int val) {
        int slow = 0;
        int fast = 0;

        while (fast < nums.length) {
            if (nums[fast] != val) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }

        return slow;
    }
}
